package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> setmeals = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(setmeals.getTotal(),setmeals.getResult());
    }
    @Transactional
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        //添加套餐
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        //添加菜品和套餐之间的关联
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Transactional
    @Override
    public void removeBatch(List<Long> ids) {

        //首先查询套餐状态，正在出售则不能删除
        List<Setmeal> setmeals = setmealMapper.getByIds(ids);
        if(setmeals!=null){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //删除套餐
        setmealMapper.deleteByIds(ids);
        //删除套餐和菜品关联数据
        setmealDishMapper.deleteByIds(ids);
    }

    @Override
    public SetmealVO getById(Long id) {
        //获取套餐基本信息
        SetmealVO setmealVO = setmealMapper.getById(id);
        //获取关联的菜品信息
        List<SetmealDish> setmealDishes=setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Transactional
    @Override
    public void updateWithDishs(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改菜品基本信息
        setmealMapper.update(setmeal);
        //删除原有关联菜品信息
        List<Long> dishId = new LinkedList<>();
        dishId.add(setmeal.getId());
        setmealDishMapper.deleteByIds(dishId);
        //插入新菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public void startOrStop(Long id,Integer status) {
        //查看当前套餐是否包含未启售的菜品
        if(status==1){
            //获取套餐包含菜品的id
            List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
            List<Long> dishIds=new ArrayList<>();
            setmealDishes.forEach(setmealDish -> {
                dishIds.add(setmealDish.getDishId());
            });
            List<Dish> dishes = dishMapper.getByIdsAndStatus(dishIds,0);
            if(dishes!=null&&dishes.size()>0){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    @Override
    public List<DishItemVO> getDishsWithDetailBySetmealId(Long id) {
        List<DishItemVO> dishItemVOS = setmealMapper.getDishsBySetmealId(id);
        return dishItemVOS;
    }

    @Override
    public List<DishVO> getDishsByCategoryId(Long categoryId) {
        List<DishVO> dishVOS = dishMapper.getByCategoryId(categoryId);
        return dishVOS;
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> setmeals = setmealMapper.list(setmeal);
        return setmeals;
    }
}
